<template>
  <!-- 轮播图 -->
  <el-carousel height="450px">
    <el-carousel-item v-for="(banner,index) in banners" :key="index">
     <el-image :src="banner"></el-image>
    </el-carousel-item>
  </el-carousel>
<!-- 广告位 -->
  <div class="ad">
    <ul>
      <li>
        <el-image src="/src/assets/img_1.png"/>
      </li>
      <li>
        <el-image src="/src/assets/img_1.png"/>
      </li>
      <li>
        <el-image src="/src/assets/img_1.png"/>
      </li>
      <li>
        <el-image src="/src/assets/img_1.png"/>
      </li>
    </ul>
  </div>
<!-- 推荐的商品 -->
  <div class="recom" v-for="(category,index) in parentList" :key="index" >
    <div class="categoryName">{{category.name}}</div>
    <div class="goodsList">
      <ul>
        <li v-for="(goods, index) in category.goodsList" :key="index" @click="toGoodsView(goods.id)">
          <div class="pic"><el-image :src="`${SERVER_ADDR}/goods/pic/${goods.picList[0].url}`" /></div>
          <div class="name">{{ goods.name }}</div>
          <div class="dscp">{{ goods.dscp }}</div>
          <div class="price">￥ {{ goods.price }}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup>
  import {ref} from "vue";
  import categoryApi from "@/api/CategoryApi.js";
  import {useRouter} from "vue-router";
  import {useGoodsStore} from "@/stores/goods.js";

  const goodStore = useGoodsStore()
  const  router = useRouter()
  const SERVER_ADDR = ref(import.meta.env.VITE_SERVER_ADDR);

  const banners = ref([
    "/src/assets/banner/banner3.png",
    "/src/assets/banner/banner2.png",
    "/src/assets/banner/banner1.png",
  ])
  const parentList = ref([]);
  //1.获取父分类 2.上架
  function getParent(){

    const condition = {
      parentId: 0,
      status: 1
    };
    console.log(goodStore.goodsInfo)
    categoryApi.selectByPage(condition)
        .then(resp => {
          parentList.value = resp.data;
          console.log(resp.data)
        })
  }
  function toGoodsView(id){
    router.push({
      path:'/user/goods',//跳转到的位置，值和页面路由中配置的路径相同
      query:{
        id
      }
    });
  }
  getParent();
</script>

<style scoped>
.recom{
  margin-bottom: 20px;
}
.recom .categoryName{
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 5px;
}
.recom .goodsList ul li{
  width: 224px;
  float: left;
  margin-right: 20px;
  margin-bottom: 20px;
  text-align: center;
  background-color: #EEE;
  cursor: pointer;
}
.recom .goodsList ul li:hover{
  box-shadow: 0 0 20px #000;
}
.recom .goodsList ul li:nth-child(5n){
  margin-right: 0;
}
.name,.dscp,.price{
  line-height: 25px;
}
.recom .name{
  font-size: 16px;
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.recom .dscp{
  color: #AAA;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.recom .price{
  font-size: 14px;
  color: var(--theme-color);
}
.ad{
  margin: 20px 0;
}
  .ad ul li {
    float:left;
    width: 285px;
    margin-right: 20px;
  }
  .ad ul li:nth-child(4n){
    margin-right: 0px;
  }
</style>